// $Id: ErrorView.java 79 2012-01-03 20:12:23Z huijzer@gmail.com $
package lime.workflow.view;

import lime.model.Workitem;
import lime.model.WorkitemError;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class ErrorView extends AbstractWorkitemListenerView {

	public static final String ID = "lime.workflow.view.error";

	// Widgets
	private Text resultText;

	@Override
	public void createPartControl(Composite parent) {
		resultText = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		resultText.setFont(JFaceResources.getTextFont());
		resultText.setEditable(false);
	}

	@Override
	public void setFocus() {
		resultText.setFocus();
	}

	@Override
	protected void update(final Workitem workitem) {

		final Thread myThread = new Thread("MyThread") {
			public void run() {
				String workitemId = workitem.getObjectId();
				final WorkitemError error = getWorkflowService().getWorkitemError(workitemId);
				getSite().getShell().getDisplay().asyncExec(new Runnable() {
					public void run() {
						setContentDescription(workitem.getProcessName() + " :: " + workitem.getActivityName());
						if (error.getResult() != null) {
							resultText.setText(error.getResult());
						} else {
							resultText.setText("No result text.");
						}
					}
				});
			}
		};
		myThread.start();

	}

}
